package com.example.bobo.yuejian322.Adapter;


import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.bobo.yuejian322.Activity.PeixunDetialActivity;
import com.example.bobo.yuejian322.Fragment.ViewHolder.PeixunVierHolder;
import com.example.bobo.yuejian322.R;
import com.example.bobo.yuejian322.bean.Peixun;
import com.example.bobo.yuejian322.bean.PeixunList;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;


public class PeixunListAdapter extends RecyclerView.Adapter    {


    private List<Peixun> peixunListList = new ArrayList<>();
    private Context context;//上下文对象
    private LayoutInflater layoutInflater;
    //构造函数
    public PeixunListAdapter(Context context, List<Peixun> peixunListList ){
        this.context=context;//保存上下文
        layoutInflater=LayoutInflater.from(context);
        this.peixunListList.addAll(peixunListList);//显示数据

    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = layoutInflater.inflate(R.layout.item_peixun,parent,false);

        PeixunVierHolder holder=new PeixunVierHolder(itemView);

        return holder;
    }
    //绑定数据
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        PeixunVierHolder peixunVierHolder = (PeixunVierHolder) holder;
        Peixun peixunList  = peixunListList.get(position);


        //获取具体的数据值


        String Intro = peixunList.getName();
        String pic = peixunList.getThumb();

        final   String  id =peixunList.getId();
        peixunVierHolder.peixun_intro.setText(Intro);



        if (pic != "") {
            Picasso.with(context).load("http://172.24.10.175/workout/Uploads/"+pic).
                    into(peixunVierHolder.peixun_image);
        }


        //页面跳转
        peixunVierHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
 //启动新的Activity—店铺详情（菜谱列表）。

                Intent intent = new Intent(context, PeixunDetialActivity.class);
                //传值
                intent.putExtra("peixunID", id);
                //在适配器里调用StarActivity方法
                context.startActivity(intent);
            }
        });

    }

    //显示条目的数量
    @Override
    public int getItemCount() {
        return peixunListList.size();
    }


}
